using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace BoOnce.GameLogic.Entities
{
    class Countdown : HUD
    {
        #region Private Attributes
        TimeSpan _ElapsedTime;
        Boolean _IsOver;
        SpriteFont _Font;
        Vector2 _Position;

        TimeSpan TIMEOUT;
        TimeSpan TIMEWARNING;
        #endregion

        #region Constructor
        public Countdown()
        {
        }
        #endregion

        #region Public Functions
        public void Start(TimeSpan time)
        {
            TIMEOUT = new TimeSpan(0);
            TIMEWARNING = new TimeSpan(0, 0, 15);
            _Font = Game.Content.Load<SpriteFont>("Fonts/Comic14");
            _ElapsedTime = time;
            _IsOver = false;
            _Position = new Vector2(0,40f);
            
        }

        public Boolean IsOver { get { return _IsOver; } }             
        #endregion

        #region DrawableEntity
        public override void QuickDraw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            String timer = _ElapsedTime.Minutes + ":" + _ElapsedTime.Seconds;

            Color c;
            float scale = 0.8f;
            if (TIMEWARNING > _ElapsedTime)
            {
                c = Color.Red;
                scale = 2.6f - _ElapsedTime.Seconds*0.1f ;
            }
            else
            {
                c = Color.White;
            }

            Vector2 stringSize = _Font.MeasureString(timer);
            _Position.X = (Settings.SCREEN_X - stringSize.X) / 2f;
            stringSize /= 2;
            spriteBatch.DrawString(_Font, timer, _Position, c, 0f, stringSize, scale, SpriteEffects.None, 0);
        }

        public override void Update(GameTime gameTime)
        {
            if (!IsOver)
                _ElapsedTime -= gameTime.ElapsedGameTime;

            if (_ElapsedTime <= TIMEOUT)
                _IsOver = true;
        }
        #endregion
    }
}
